﻿cmake_minimum_required(VERSION 2.8.12.1)

project(hqwxmetrics)

include(../ci_build/cmake/CMakeUtils.txt)

if(NOT PLATFORM)
    set(PLATFORM "client")
endif()

if (${PLATFORM} STREQUAL "server")
    set(METRICS_HEADER hqwxmetrics_server.hpp)
    add_definitions(-DHQWX_METRICS_SERVER)
else()
    set(METRICS_HEADER hqwxmetrics_client.hpp)
endif()


# config
## search header
set(SUB_MODULES utils event_builder events impl model processor sender)
include_directories_recurse("${SUB_MODULES}")

## depends
if(NOT HQWX_BASE_DIR)
    set(HQWX_BASE_DIR "../depends/hqwxbase" CACHE STRING "" FORCE)
endif()
message(STATUS HQWX_BASE_DIR=${HQWX_BASE_DIR})

if(NOT ZLIB_SDK_DIR)
    set(ZLIB_SDK_DIR "../depends/zlib" CACHE STRING "" FORCE)
endif()
message(STATUS ZLIB_SDK_DIR=${ZLIB_SDK_DIR})

if(NOT OPENSSL_SDK_DIR)
    set(OPENSSL_SDK_DIR "../depends/openssl" CACHE STRING "" FORCE)
endif()
message(STATUS OPENSSL_SDK_DIR=${OPENSSL_SDK_DIR})

if(NOT CURL_SDK_DIR)
    set(CURL_SDK_DIR "../depends/curl" CACHE STRING "" FORCE)
endif()
message(STATUS CURL_SDK_DIR=${CURL_SDK_DIR})

include_directories(${HQWX_BASE_DIR}/include/hqwxbase)
include_directories(${HQWX_BASE_DIR}/include)
include_directories(${ZLIB_SDK_DIR}/include)
include_directories(${CURL_SDK_DIR}/include)
include_directories(${OPENSSL_SDK_DIR}/include)
include_directories(3rd/gzip/include)
include_directories(3rd/spdlog/include)
include_directories(3rd)


set(PUBLIC_HEADERS
    ${METRICS_HEADER}
    hqwxmetrics_version.h
    hqwxmetrics_export.h
    model/hqwxmetrics_types.hpp
    model/hqwxmetrics_event.hpp
    utils/hqwxmetrics_system_status.hpp
    event_builder/hqwxmetrics_eventbuilder.hpp
    event_builder/hqwxmetrics_eventbuilder_macro.hpp
    event_builder/hqwxmetrics_eventbuilder_template.hpp
    )

file_glob_ex(SRC_FILES
    TARGETS
        *.c *.cc *.cpp
    INCLUDES
        ${PROJECT_SOURCE_DIR}
    EXCLUDES
        ${PROJECT_SOURCE_DIR}/3rd
        ${PROJECT_SOURCE_DIR}/platform
    RELATIVE
        ${PROJECT_SOURCE_DIR}
    )

if(APPLE)
    if (DEFINED IOS_PLATFORM)
        include_directories_recurse(./platform/ios)
        file_glob_ex(IOS_SRC_FILES
            TARGETS
                *.c *.cc *.cpp *.m *.mm
            INCLUDES
                ${PROJECT_SOURCE_DIR}/platform/ios
            RELATIVE
                ${PROJECT_SOURCE_DIR}
            )

        list(APPEND SRC_FILES ${IOS_SRC_FILES})
    else()
        include_directories_recurse(./platform/mac)
        file_glob_ex(MAC_SRC_FILES
            TARGETS
                *.c *.cc *.cpp *.m *.mm
            INCLUDES
                ${PROJECT_SOURCE_DIR}/platform/mac
            RELATIVE
                ${PROJECT_SOURCE_DIR}
            )

        list(APPEND SRC_FILES ${MAC_SRC_FILES})
    endif()

    add_library(${PROJECT_NAME} ${LIBRARYTYPE} ${SRC_FILES})
    if(BUILD_DLL)
        target_link_libraries(${PROJECT_NAME} 
            z 
            debug ${HQWX_BASE_DIR}/lib/libhqwxbase_d.dylib optimized ${HQWX_BASE_DIR}/lib/libhqwxbase.dylib
            )
    endif()

elseif(ANDROID)
    include_directories(.)
    include_directories(${HQWX_BASE_DIR}/${ANDROID_ABI}/include/hqwxbase/jni)
    include_directories(${HQWX_BASE_DIR}/${ANDROID_ABI}/include/hqwxbase)
    include_directories(${HQWX_BASE_DIR}/${ANDROID_ABI}/include)
    include_directories(${CURL_SDK_DIR}/${ANDROID_ABI}/include)

    include_directories_recurse(./platform/android)

    link_directories(${HQWX_BASE_DIR}/${ANDROID_ABI}/libs)
    find_library(z-lib z)

    file_glob_ex(ANDROID_SRC_FILES
        TARGETS
            *.c *.cc *.cpp
        INCLUDES
            ${PROJECT_SOURCE_DIR}/platform/android
        RELATIVE
            ${PROJECT_SOURCE_DIR}
        )

    list(APPEND SRC_FILES ${ANDROID_SRC_FILES})
    add_library(${PROJECT_NAME} ${LIBRARYTYPE} ${SRC_FILES})

    set(HQWXBASE_LIB hqwxbase)

    target_link_libraries(${PROJECT_NAME} ${HQWXBASE_LIB} ${z-lib})

elseif(MSVC)
    add_definitions(-DHQWX_METRICS_DLL_BUILD)
    include_directories(.)
    include_directories_recurse(./platform/win)

    link_directories(${HQWX_BASE_DIR}/lib)
    link_directories(${ZLIB_SDK_DIR}/lib)
	link_directories(${CURL_SDK_DIR}/lib)
    link_directories(${OPENSSL_SDK_DIR}/lib)

    file_glob_ex(WIN_SRC_FILES
        TARGETS
            *.h *.hpp *.c *.cc *.cpp
        INCLUDES
            ${PROJECT_SOURCE_DIR}/platform/win
        RELATIVE
            ${PROJECT_SOURCE_DIR}
        )

    list(APPEND PUBLIC_HEADERS platform/win/system.h)
    list(APPEND SRC_FILES ${WIN_SRC_FILES})

    add_library(${PROJECT_NAME} ${LIBRARYTYPE} ${SRC_FILES})

    set(HQWXBASE_LIB debug hqwxbase_d optimized hqwxbase)

    target_link_libraries(${PROJECT_NAME}
        ws2_32.lib
        ${HQWXBASE_LIB}
        zlibstatic
    )

    install(FILES $<TARGET_PDB_FILE:${PROJECT_NAME}>
        DESTINATION pdb OPTIONAL
    )
endif()

## package sdk
install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION ${PROJECT_NAME}/bin
    LIBRARY DESTINATION ${PROJECT_NAME}/${LIB_DIR_NAME}
    ARCHIVE DESTINATION ${PROJECT_NAME}/${LIB_DIR_NAME}
)

install(FILES ${PUBLIC_HEADERS}
    DESTINATION ${PROJECT_NAME}/include/${PROJECT_NAME})

install(DIRECTORY events
    DESTINATION ${PROJECT_NAME}/include/${PROJECT_NAME})
